layui.use(['form','layer','element','laydate'],function(){
	
	var form = layui.form,layer = layui.layer,element=layui.element,laydate = layui.laydate;
	
	function showsplan(val){
		if(val == 3){
			$("#splan").show();
		}else
			$("#splan").hide();
	}
	
	form.on('select(scheduleFrequency)',function(data){
		var val = data.value;
		showCron(val);
	});


	function showCron(val){
		if(val == 1){
			$("#scheduleCron1").show();
			$("#scheduleCron").hide();
		}else if(val == 2){
			$("#scheduleCron").show();
			$("#scheduleCron1").hide();
		}
	}


	
	initTask();
	
	function initTask(){
		
		var taskId = $("#taskId").val();
		ajaxPost('../task/getTaskById',{taskId:taskId},
		         function(data){
					 initTaskData(data);
				 },
				 function(data){
					 parent.layer.alert(data.errorMsg, {
										icon: 5,
										title: "错误"
									});
				 });
		
		
	}
	
	
	function initTaskData(data){
		
		var taskdata = data.data;
		$("#taskName").val(taskdata.taskName);
		
		$("#appId").val(taskdata.appId);
		
		
		$("#testStatus").val(taskdata.testStatus);
		
		$("#planDate").val(taskdata.planStartDateStr+' 到 '+taskdata.planEndDateStr);
		
		$("#testType").val(taskdata.testType);
		
		$("#tryNum").val(taskdata.tryNum);
		
		$("#scheduleType").val(taskdata.scheduleType);
		showsplan(taskdata.scheduleType);
		if(taskdata.scheduleType == 3){
			$("#scheduleFrequency").val(taskdata.scheduleFrequency);
			var cron = taskdata.scheduleCron.split(" ");
			showCron(taskdata.scheduleFrequency);
			if(taskdata.scheduleFrequency == 2){
				$("#scheduleCron").val(addZero(cron[2])+":"+addZero(cron[1])+":"+addZero(cron[0]));
			}else if(taskdata.scheduleFrequency == 1){
				$("#scheduleCron1").val(cron[6]+"-"+addZero(cron[4])+"-"+addZero(cron[3])+" "+addZero(cron[2])+":"+addZero(cron[1])+":"+addZero(cron[0]));
			}
		}
		selCase(data.casedata);
		selDevice(data.devicedata);
		form.render("select");
	
	}
	
	
	function addZero(val){
		if(parseInt(val,10) > 9)
			return val;
		else return "0"+val;
	}
	
	
	function selCase(data){
		$("#casedg").empty();
		var caseIds = [];
		for(var i = 0 ;i < data.length;i++){
			var casedata = data[i];
			$("#casedg").append("<tr><td>"+casedata.caseName+"</td>"+
                    "<td>"+casedata.appName+"</td>"+
					  "<td>"+(casedata.caseType == 1?"正面":"负面")+"</td>"+
					  "</tr>");
			caseIds.push(casedata.caseId);
		}
		$("#caseIds").val(caseIds.join(","));
	}
	
	
	function selDevice(data){
		$("#devicedg").empty();
		var deviceIds = [];
		for(var i = 0 ; i < data.length;i++){
			var devicedata = data[i];
			$("#devicedg").append("<tr><td>"+devicedata.serialNumber+"</td>"+
				                      "<td>"+devicedata.platform+"</td>"+
									  "<td>"+devicedata.brand+"</td>"+
									  "<td>"+devicedata.model+"</td>"+
									  "</tr>");
			deviceIds.push(devicedata.deviceId);
		}
		$("#deviceIds").val(deviceIds.join(","));
	}
	
	
});